package com.hongye.proprietary.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hongye.common.entity.LeaseHomeEntity;
import com.hongye.common.entity.SaleHomeEntity;
import com.hongye.common.utils.PageUtils;
import com.hongye.common.utils.R;
import com.hongye.proprietary.config.PathResource;
import com.hongye.proprietary.controller.vo.LoginVo;
import com.hongye.proprietary.entity.AgentUserEntity;
import com.hongye.proprietary.fegin.HouseFegin;
import com.hongye.proprietary.service.AgentUserService;
import com.hongye.proprietary.vo.LeaderboardVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 经纪人表
 *
 * @author lins
 * @email 695565697@qq.com
 * @date 2021-10-14 11:16:47
 */
@Slf4j
@RestController
@RequestMapping("proprietary/agentuser")
public class AgentUserController {
    @Autowired
    private AgentUserService agentUserService;
    @Autowired
    private PathResource pathResource;

    @Autowired
    HouseFegin houseFegin;

    /*
    * 启用
    * */
    @RequestMapping("statusChange")
    public R statusChange(@RequestBody AgentUserEntity agentUserEntity){
        agentUserService.updateById(agentUserEntity);
        return R.ok("修改状态成功");
    }
    //经纪人
    // 验证码登录
    @RequestMapping("/login")
    public R sendMsg(@RequestBody LoginVo loginVo){
        return agentUserService.login(loginVo);
    }

       /*
        经纪人发送短信验证
     */
       @RequestMapping("/sendMsg")
       public R sendMsg(String phone){
           if (pathResource.isNullNO(phone)){
               //手机号正则校验
               if (pathResource.isMobileNO(phone)){
                   return agentUserService.sendMsg(phone);
               }
               return R.error("手机号格式错误");
           }
           return R.error("手机号不能为空");
       }
    /*
    经纪人详情页面
    售房接口
     */
    @RequestMapping("/saledetails/{houseagent}")
    public R saledetails(@PathVariable("houseagent")Integer houseagent){
     AgentUserEntity  saledetails=agentUserService.saledetails(houseagent);
        List<SaleHomeEntity> salelist = houseFegin.salelist(houseagent);
        return R.ok().put("data",saledetails).put("salelist",salelist);
    }



    /*
    经纪人详情页面
    租房接口
     */
    @RequestMapping("/rentingdetails/{houseagent}")
    public R rentingdetails(@PathVariable("houseagent")Integer houseagent){
        AgentUserEntity  saledetails=agentUserService.rentingdetails(houseagent);
        List<LeaseHomeEntity> leaseHomeEntities = houseFegin.rentinglist(houseagent);
        return R.ok().put("data",saledetails).put("leaseHomeEntities",leaseHomeEntities);
    }


    /*
    排行榜
     */
    @RequestMapping("/leaderboardlist")
    public R  leaderboardlist(){
        List<LeaderboardVo>  leaderboardVos=agentUserService.leaderboardlist();
        return R.ok().put("data",leaderboardVos);
    }

    @RequestMapping("/hotlist")
    public List<LeaderboardVo>  hotlist(){
        List<LeaderboardVo>  leaderboardVos=agentUserService.leaderboardlist();
        return leaderboardVos;
    }


    //房源 获取经纪人信息
    @RequestMapping("/agentlist")
    public List<AgentUserEntity> agentlist(){
        return agentUserService.list();
    }

    //登录
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = agentUserService.queryPage(params);

        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{agentId}")
    public AgentUserEntity info(@PathVariable("agentId") Integer agentId){
		return agentUserService.getById(agentId);


    }
    //修改经纪人状态
    @RequestMapping("updateagent")
    public R updateagent(@RequestBody AgentUserEntity agentUserEntity){
        if (agentUserEntity.getAgentId()!=null){
            agentUserService.updateById(agentUserEntity);
            return R.ok();
        }
        return R.error();
    }
    //删除经纪人
    @RequestMapping("delAgent")
    public R delAgent(@RequestParam("aid") Integer aid){
        agentUserService.removeById(aid);
        return R.ok();
    }

    @RequestMapping("updateAgentStatus")
    public R updateAgentStatus(@RequestParam("aid") Integer aid,@RequestParam("status") Integer status){

        agentUserService.updateAgentStatus(aid,status);
        return R.ok();
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated @RequestBody AgentUserEntity agentUser){
		agentUserService.save(agentUser);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AgentUserEntity agentUser){
		agentUserService.updateById(agentUser);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{agentId}")
    public R delete(@PathVariable("agentId") Integer agentId){
		agentUserService.removeById(agentId);

        return R.ok();
    }

}
